<?php
namespace app\shop\model;
use app\BaseModel;
use app\integral\model\Integral;
use think\facade\Db;
use app\shop\model\IndexShopSpePrice;



class IndexShopCollage extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_collage';

    protected static function init()
    {
        //TODO:初始化内容
    }

    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取拼团列表
     */
    public function collageList($dis){

        $list = $this->where($dis)->select()->toArray();

//        $integral_model = new Integral();


        if(!empty($list)){

            foreach ($list as &$value){

                $spe_model = new IndexShopSpePrice();

                $spe       = $spe_model->SpePriceFind(['id'=>$value['spe_price_id'],'status'=>1]);

                if(!empty($spe)){

                    $value['spe_stock'] = $spe['stock'];

                    $value['title']     = $spe['spe_name_text'];

                    $value['spe_price'] = $spe['price'];

                }else{

                    unset($value);

                }

//                $integral_atv = $integral_model->buyLimit($value['goods_id'],$value['spe_price_id']);
//                //必须积分活动不允许拼团
//                if(empty($integral_atv)||$integral_atv['buy_limit']==1){
//
//                    $lists[] = $value;
//
//                }

            }

        }

        return $list;
    }

    /**
     * @param $dis
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取拼团信息
     */
    public function collageInfo($dis){
       $data = $this->where($dis)->find();
       return !empty($data)?$data->toArray():$data;

    }

    /**
     * @param $dis
     * @param $data
     * @return IndexShopCollageList
     * 编辑拼团信息
     */

    public function collageUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }

    /**
     * @param $dis
     * @return bool
     * @throws \Exception
     * 删除拼团信息
     */

    public function collageDel($dis){
        $res = $this->where($dis)->delete();
        return $res;
    }


    public function getCollage($filter)
    {
        $result = $this->where($filter)->find();
        if(!empty($result)) $result = $result->toArray();
        return $result;
    }


}